package bgate.hung.workerclass;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import bgate.hung.framework.Sprite;

public class Card extends Sprite {
	public static final int STATIC = 0;
	public static final int MOVING = 1;
	public static final int REMOVING = 3;
	private int id;
	public int chat;
	public int so;
	public Bitmap frame;
	public int  state = STATIC; 
	private Bitmap sign_bitmap;
	public boolean sign = false;
	private int t = 0;
	private int soureX;
	private int soureY;
	private int distX;
	private int distY;

	public Card(int x, int y, int timePerFrame, int numbsFrame, int id) {
		super(x, y, timePerFrame, numbsFrame);
		// TODO Auto-generated constructor stub
		this.bitmap = StaticBitmap.getCardImage();
		this.sign_bitmap = StaticBitmap.getSignCardImage();
		this.width = bitmap.getWidth() / numbsFrame;
		this.height = bitmap.getHeight();
		super.initialize();
		this.id = id;
		chat = id / 13;
		so = id % 13;
		setFrame(id);
		frame = getImageFrame(id);
		soureX = x;
		soureY = y;
	}
	
	public Card(int id) {
		this.id = id;
		chat = id / 13;
		so = id % 13;
	}

	public void setDist(int dx, int dy) {
		distX = dx;
		distY = dy;
	}

	public int getId() {
		return id;
	}
	
	public void move(long time) {
		if (time > lastTimeFrameDisplayed + timePerFrame) {
			lastTimeFrameDisplayed = time;
			t++;
			if (getY() != distY && getX() != distX) {
				setX(soureX + ((distX - soureX) * t) / 10);
				setY(soureY + ((distY - soureY) * t) / 10);
			} else {
				setX(distX);
				setY(distY);
				state = REMOVING;
			}
		}
	}

	public boolean getIsChoise(int t) {
		if (getY() < t)
			return true;
		else
			return false;
	}

	public void changeId(int id) {
		this.id = id;
		setFrame(id);
	}

	public int getCenterX() {
		return (getX() + this.width / 2);
	}

	public int getCenterY() {
		return (getY() + this.height / 2);
	}

	public int getWith() {
		return this.width;
	}

	public int getHeight() {
		return this.height;
	}

	public boolean isInsert(int tx, int ty) {
		if ((tx > getX()) && (tx < (getX() + this.width)) && (ty > getY())
				&& (ty < (getY() + this.height))) {
			return true;
		} else {
			return false;
		}
	}

	public void draw(Canvas canvas) {
		super.draw(canvas);
		if (sign)
			canvas.drawBitmap(sign_bitmap, getX(), getY() + height / 3, null);
	}

	public boolean isTouched(int x, int y) {
		int mX = 0;
		int mY = 0;

		mX = x;
		mY = y;
		if ((mX >= getX()) && (mX <= getX() + width) && (mY >= getY())
				&& (mY <= getY() + height)) {
			int arcX, arcY;
			arcX = mX - getX();
			arcY = mY - getY();
			int pattern = 0;

			try {
				pattern = frame.getPixel(arcX, arcY);
			} catch (Exception e) {
				System.out.println("EXCEED DIMENSION");
			}
			int alpha = pattern >> 24;
			if (alpha == 0) {
				return false;
			} else {
				return true;
			}
		} else
			return false;
	}
}
